import asyncio
import time
import socketio

loop = asyncio.get_event_loop()
sio = socketio.AsyncClient()
start_timer = None

@sio.event
async def connect():
    print('connected to server')
    global start_timer
    start_timer = time.time()
    await sio.emit('data_from_client', "I have trained the local model and finished aggregation time.")


@sio.event
async def receipt_from_server():
    global start_timer
    latency = time.time() - start_timer
    print('latency is {0:.2f} ms'.format(latency * 1000))
    await sio.sleep(1)

@sio.event
async def start_server():
    await sio.connect('http://localhost:5000')
    await sio.wait()


if __name__ == '__main__':
    loop.run_until_complete(start_server())
